/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.PlainUserBo;
import top.continew.admin.hanfu.domain.dto.PlainUserDto;
import top.continew.admin.hanfu.domain.query.PlainUserQuery;
import top.continew.admin.hanfu.domain.vo.PlainUserVo;
import top.continew.admin.hanfu.service.impl.PlainUserServiceImpl;

import java.util.ArrayList;

/**
 * @author xieling
 * @version 1.0
 */
@RestController
@RequestMapping("/user_all")
@RequiredArgsConstructor
@Slf4j
public class PlainUserController {

    private final PlainUserServiceImpl plainUserService;

    /**
     * 查询 by id
     */

    @GetMapping("/{id}")
    public R QueryUserById(@PathVariable("id") Integer id) {

        return R.success(plainUserService.getById(id));

    }

    /**
     * 查询接口
     */
    @PostMapping("query")
    public R QueryUser(@RequestBody PlainUserQuery plainUserQuery) {

        Page<PlainUserVo> userListByCondition = plainUserService.getUserListByCondition(plainUserQuery);

        /**
         * 格式化时间格式
         */
        userListByCondition.getRecords().forEach(item -> {
            item.setUpdateTime(DateTime.of(Long.parseLong(item.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(userListByCondition);

    }

    /**
     * 新增接口
     */
    @PostMapping("")
    public R addUser(@RequestParam("userImg") MultipartFile userImg,
                     @RequestParam("userName") String userName,
                     @RequestParam("userInfo") String userInfo,
                     @RequestParam("userJob") String userJob,
                     @RequestParam("status") Integer status,
                     @RequestParam("uploader") String uploader) {

        PlainUserBo plainUserBo = new PlainUserBo();

        plainUserBo.setFile(userImg);
        plainUserBo.setUserInfo(userInfo);
        plainUserBo.setUploader(uploader);
        plainUserBo.setStatus(status);
        plainUserBo.setUserJob(userJob);
        plainUserBo.setUserName(userName);

        int i = plainUserService.addUser(plainUserBo);

        return i == 1 ? R.success() : R.fail("");
    }

    /**
     * 更新用户接口 信息
     */
    @PutMapping("")
    private R updateUser(@RequestBody PlainUserDto plainUserDto) {

        int i = plainUserService.updateUser(plainUserDto);

        return i == 1 ? R.success() : R.fail("");

    }

    /**
     * 更新用户头像接口
     */
    @PutMapping("img")
    private R updateUserImg(@RequestParam("userImg") MultipartFile userImg,
                            @RequestParam("userId") Integer id,
                            @RequestParam("uploader") String uploader) {

        PlainUserBo plainUserBo = new PlainUserBo();

        plainUserBo.setId(id);
        plainUserBo.setFile(userImg);
        plainUserBo.setUploader(uploader);

        int i = plainUserService.updateImg(plainUserBo);

        return i == 1 ? R.success() : R.fail("");

    }

    /**
     * 删除
     */
    @DeleteMapping("")
    private R delUser(@RequestBody ArrayList<Integer> ids) {
        boolean b = plainUserService.removeByIds(ids);

        return b ? R.success() : R.fail("");
    }
}
